首先我們建立一個訊息匡
let newMessage = NFCNDEFMessage(records: [
NFCNDEFPayload(format: .unchanged,
type: "com.example.payload12".data(using: .utf8)!,
identifier: "example-identifier12".data(using: .utf8)!,
payload: "訊息".data(use:.utf8)
])
func writeToNFCTag(_ message: NFCNDEFMessage) async {
guard let tag = detectedTag else {
print("未檢測到 NFC 標籤")
return
}
do {
try await nfcManager.write(tag: tag, data: message)
} catch {
Alert.showAlert(title: "錯誤",
message: "寫入 NFC 標籤失敗:\(error.localizedDescription)",
vc: self,
confirmTitle: "確定") { [self] in
nfcManager.scanNFC()
}
}
}
func write(tag: NFCNDEFTag, data: NFCNDEFMessage) async throws {
try await tag.writeNDEF(data)
}
```
然後資料帶入就可以寫入了